﻿using System;
using System.Collections;
using System.IO;
using System.Text;
using UnityEngine.Networking;
using Yoozoo.Mars.Got;
using Yoozoo.Tools.Debugger;

namespace Yoozoo.Gameplay.GameUtils
{
    public class DebugForLua
    {
        public static bool enablePrefix
        {
            get => Debug.enablePrefix;
            set => Debug.enablePrefix = value;
        }

        public static bool enableFrameCount
        {
            get => Debug.enableFrameCount;
            set => Debug.enableFrameCount = value;
        }

        public static bool enableLuaStackTrace
        {
            get => Debug.enableLuaStackTrace;
            set => Debug.enableLuaStackTrace = value;
        }

        public static bool enableDumpTree
        {
            get => Debug.enableDumpTree;
            set => Debug.enableDumpTree = value;
        }

        public static int LogLevel
        {
            get => Debug.LogLevel;
            set => Debug.LogLevel = value;
        }

        public static Action<string, bool> OnEditorSwitchLog
        {
            get => Debug.OnEditorSwitchLog;
            set => Debug.OnEditorSwitchLog = value;
        }

        public static bool IsEditorMode()
        {
            return Debug.IsEditorMode();
        }

        public static bool GetLocalLogState(string moduleName)
        {
            return Debug.GetLocalLogState(moduleName);
        }

        public static void _LOG_TYPE_(int logType, string prefix, object message, int level, UnityEngine.Object context, int features = 0)
        {
            Debug._LOG_TYPE_(logType, prefix, message, level, context, features);
        }

        public static void SwitchLogModule(string moduleName, bool isOn)
        {
            Debug.SwitchLogModule(moduleName, isOn);
        }

        public static void SaveLogLocalState()
        {
            Debug.SaveLogLocalState();
        }
        
        public static void UploadLog(string url)
        {
            string logFilePath = Log.LogFilePath;
            if (!string.IsNullOrEmpty(logFilePath) && File.Exists(logFilePath))
            {
                Log.CloseFileStream();
                var texBytes = File.ReadAllBytes(logFilePath);
                Log.OpenFileStream();
                Timers.inst.StartCoroutine(Upload(url, texBytes));
            }
            else
            { 
                UnityEngine.Debug.LogError("日志上传失败，没有日志文件");
            }
        }
        
        static IEnumerator Upload(string url, byte[] bytes, Action success = null, Action fail = null)
        {
            using (UnityWebRequest www = new UnityWebRequest(url, "POST"))
            {
                www.uploadHandler = new UploadHandlerRaw(bytes);
                www.downloadHandler = new DownloadHandlerBuffer();
                //www.SetRequestHeader("Content-Type", "image/png");
                yield return www.SendWebRequest();
                if (IsUploadSucc(www))
                {
                    success?.Invoke();
                    UnityEngine.Debug.Log("上传日志成功：" + url);
                }
                else
                {
                    UnityEngine.Debug.LogError("上传日志失败：" + url);
                    fail?.Invoke();
                }
            }
        }
        
        private static bool IsUploadSucc(UnityWebRequest www)
        {
            var result = www.downloadHandler?.text;
            if (result == "OK")
            {
                return true;
            }
            return false;
        }

        public static bool IsDebugPackage()
        {
            return Debug.isDebugBuild;
        }
    }
}
